from apps.common.base.serializers import *
from apps.merchant.sku.models import *


class SkuBaseSerializer(BaseSerializer):
    class Meta:
        model = Sku
        fields = "__all__"


class SkuModelSerializer(BaseSerializer):

    itemModel = serializers.SerializerMethodField()
    def get_itemModel(self, obj):
        try:
            return {'id': obj.item.id, 'name': obj.item.name, 'cateName': obj.item.cate.name if obj.item.cate else '-', 'activityName': obj.item.activity.name if obj.item.activity else '-'}
        except:
            return {}
        
    priceModel = serializers.SerializerMethodField()
    def get_priceModel(self, obj):
        user = None
        if self.context.get('request'):
            user = self.context["request"].user
        if obj:
            sku = obj
            orderItemList = [{'item': sku.item.id, 'sku': sku.id, 'payCount': 1}]
            from apps.pay.order.sceneHandle import setPayMoney
            res = setPayMoney({'orderItemList': orderItemList}, {'scene': 'generay'}, user)
            return res.get('data')
        else:
            return {}

    class Meta:
        model = Sku
        fields = "__all__"
